import {Clazz} from "./Clazz";
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js';
import {DRACOLoader} from 'three/examples/jsm/loaders/DRACOLoader.js';
import * as THREE from 'three';
class RunCar extends Clazz {
    constructor() {
        super();
        const _this=this;
        const shadow = new THREE.TextureLoader().load('../js/components/images/runcar/ferrari_ao.png');
        const dracoLoader = new DRACOLoader();
        dracoLoader.setDecoderPath('../node_modules/three/examples/jsm/libs/draco/gltf/');

        this.position=new THREE.Vector3();
        const loader = new GLTFLoader();
        loader.setDRACOLoader(dracoLoader);
        const wheels = [];
        const bodyMaterial = new THREE.MeshPhysicalMaterial( {
            color: 0xff0000, metalness: 1.0, roughness: 0.5, clearcoat: 1.0, clearcoatRoughness: 0.03
        } );

        const detailsMaterial = new THREE.MeshStandardMaterial( {
            color: 0xffffff, metalness: 1.0, roughness: 0.5
        } );

        const glassMaterial = new THREE.MeshPhysicalMaterial( {
            color: 0xffffff, metalness: 0.25, roughness: 0, transmission: 1.0
        } );

        loader.load('../js/components/images/runcar/ferrari.glb', function (gltf) {
            const carModel = gltf.scene.children[0];

            carModel.getObjectByName('body').material = bodyMaterial;

            carModel.getObjectByName('rim_fl').material = detailsMaterial;
            carModel.getObjectByName('rim_fr').material = detailsMaterial;
            carModel.getObjectByName('rim_rr').material = detailsMaterial;
            carModel.getObjectByName('rim_rl').material = detailsMaterial;
            carModel.getObjectByName('trim').material = detailsMaterial;

            carModel.getObjectByName('glass').material = glassMaterial;

            wheels.push(
                carModel.getObjectByName('wheel_fl'),
                carModel.getObjectByName('wheel_fr'),
                carModel.getObjectByName('wheel_rl'),
                carModel.getObjectByName('wheel_rr')
            );

            // shadow
            const mesh = new THREE.Mesh(
                new THREE.PlaneGeometry(0.655 * 4, 1.3 * 4),
                new THREE.MeshBasicMaterial({
                    map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true
                })
            );
            mesh.rotation.x = -Math.PI / 2;
            mesh.renderOrder = 2;
            carModel.add(mesh);
            carModel.scale.set(100,100,100);
            carModel.position.copy(_this.position);
            _this.position=carModel.position;
            scene.add(carModel);

        });
    }

    update() {
        super.update();
    }
}

export {RunCar}